package org.ligson.hbase127;

import lombok.extern.slf4j.Slf4j;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.data.hadoop.hbase.HbaseTemplate;

@Configuration("ligson.hbase")
@Import(HBaseProperties.class)
@Slf4j
public class HBaseBootConfig {

    private final HBaseProperties hBaseProperties;

    public HBaseBootConfig(HBaseProperties hBaseProperties) {
        this.hBaseProperties = hBaseProperties;
    }

    @Bean
    public org.apache.hadoop.conf.Configuration configuration() {
        org.apache.hadoop.conf.Configuration config = HBaseConfiguration.create();
        hBaseProperties.getProperties().forEach((k, v) -> {
            log.debug("HBase配置:{}={}", k, v);
            config.set(k, v);
        });
        return config;
    }

    @Bean
    public HBasePoolFactory hBasePoolFactory(org.apache.hadoop.conf.Configuration configuration) {
        return new HBasePoolFactory(configuration);
    }

    @Bean
    public HBaseConnectionFactory hBaseConnectionFactory(HBasePoolFactory hBasePoolFactory) {
        return new HBaseConnectionFactory(hBasePoolFactory, hBaseProperties.getPoolConfig());
    }

    @Bean
    public HbaseTemplate hbaseTemplate(org.apache.hadoop.conf.Configuration configuration) {
        return new HbaseTemplate(configuration);
    }

}
